(function( $ ){
        $.fn.extend({

                shortenUrl: function(longUrl) {
		login = 'testuserforshortening';
		apiKey = 'R_db8dcbb3ba3bc504c5dab1cf23878d09';

                        // A call on an Anchor HTML element
                        if (longUrl == null)
                        {
                                // longUrl not given so use the existing href instead
                                longUrl = jQuery(this).attr('href');
                                if (longUrl == null) return; // Missing long url so do nothing
                        }
                        _shortenUrlWithBitLy(this, login, apiKey, longUrl);
                }
        });

        // Shortens an url using the bit.ly API
        function _shortenUrlWithBitLy(element, login, apiKey, longUrl)
        {
                var restApiCall = "http://api.bitly.com/v3/shorten?login=" + login + "&apiKey=" + apiKey + "&longUrl="
                        + escape(longUrl)
                        + "&format=json&callback=?";
                
                $.getJSON(restApiCall, function(data) {
                        if (data != null && data.data != null && data.data.url) {
                                $(element).attr('src', data.data.url+ ".qrcode?s=234");
                                $(element).attr('value', data.data.url);
			}

                });
        }

})( jQuery );

jQuery(document).ready(function($) {
	var cURL = window.location.href;
	$('#qrimg').shortenUrl(cURL);
	$("a.dqr").removeClass("grey");
	$('#q-h').hide();
	$('#qrcode-s').click(function() {
		jQuery('#q-h').slideToggle('fast');
		//$('a.square').addClass("grey");
		//$("a.dqr").removeClass("grey");
		//$('#qrimg').shortenUrl(cURL);
		return false;
	});

	$('a.dqr').click(function() {
		$('a.square').addClass("grey");
		$(this).removeClass("grey");
		$('#qrimg').shortenUrl(cURL);
		return false;
	});
   
   	$('a.square').click(function() {
		var gURL= $(this).attr("href");
		$('a.square').addClass("grey");
		$(this).removeClass("grey");
		$('#qrimg').shortenUrl(gURL);
		return false;
	});

});